import { getPosIDs, getUserID, loopArray, requestFunCompon } from '@/utils';
import React from 'react';
import { useRequest } from 'umi';
import _ from 'lodash';

const usePositionOperation = (curRes: any, stepType: string, thirdData: any) => {
  // 返回可选的数据
  const choiceLeng = React.useCallback(
    (treeList: any) => {
      let lengthNum: number = 0;
      let listArrData = loopArray(_.cloneDeep(treeList));
      lengthNum = isCheck(_.cloneDeep(listArrData));
      return lengthNum;
    },
    [thirdData, stepType],
  );

  // 判断可选还是不可选
  const isCheck = (checkArr: any) => {
    let cheList: any = [];
    checkArr?.forEach((item: any) => {
      if (stepType == 'thirdStep' && item?.category == 1) {
        cheList?.push(item);
      } else if (stepType == 'fourthStep' && item?.textType == 1) {
        cheList?.push(item);
      } else if (stepType == 'secondStep') {
        cheList?.push(item);
      }
    });
    return cheList?.length;
  };

  if (stepType == 'thirdStep') {
    return {
      rouleData: thirdData,
      rouleLoading: thirdData && thirdData?.length > 0 ? false : true,
      choiceLength: choiceLeng(thirdData),
    };
  } else {
    // 获取岗位下的角色树
    const posRouleTree = useRequest(
      () => {
        if (stepType == 'secondStep') {
          // 获取角色
          // let reqUrl = `/positionController/treeOfRole/${getPosIDs()}`;
          let reqUrl = `/userController/treeOfRole/${getUserID()}`;
          return requestFunCompon('GET', reqUrl);
        } else if (stepType == 'fourthStep') {
          //获取科目
          let reqUrl = `/positionController/getSubject`;
          return requestFunCompon('GET', reqUrl);
        }
      },
      {
        manual: false, //页面初始请求为 false  ,, true进入页面不会请求
        onSuccess: (res) => {
          if (res && res?.msgFlag) {
          }
        },
      },
    );

    // 处理数据
    let rouleData = React.useMemo(() => {
      return posRouleTree?.data?.obj;
    }, [posRouleTree.data]);

    return {
      rouleData: rouleData,
      rouleLoading: posRouleTree.loading,
      getRouleTreeData: posRouleTree.run,
      choiceLength: choiceLeng(posRouleTree?.data?.obj),
    };
  }
};

// 新增或者编辑的方法
// export const useAddEditPosData = (
//   curRes: any,
//   bodyParams: any,
//   addEditType: any
// ) => {

//   return {
//     // rouleLoading: posRouleTree.loading,
//     // getRouleTreeData: posRouleTree.run,
//   }
// }

// 获取详情
const useViewInfoPosData = (bodyParams: any) => {
  const posDetailInfo = useRequest(
    () => {
      let reqUrl = `/positionController/getPositionByID/${bodyParams?.id}`;
      return requestFunCompon('GET', reqUrl);
    },
    {
      manual: bodyParams && bodyParams?.id ? false : true, //页面初始请求为true
      onSuccess: (res) => {
        if (res && res?.msgFlag) {
          // message.success(res?.msg)
        }
      },
    },
  );

  // 处理数据
  let detailInfoData = React.useMemo(() => {
    return posDetailInfo?.data?.obj;
  }, [posDetailInfo.data]);

  return {
    detailInfoData: detailInfoData,
    infoLoading: posDetailInfo.loading,
    infoRefresh: posDetailInfo.run,
  };
};

// 新增接口 方法
const addPosRequest = (bodyPayload: any, reqType: string) => {
  let reqUrl = reqType == 'add' ? '/positionController/add' : reqType == 'edit' ? '/positionController/edit' : '';
  let methodType = reqType == 'add' ? 'POST' : reqType == 'edit' ? 'PUT' : '';
  if (reqUrl && methodType) {
    return requestFunCompon(methodType, reqUrl, bodyPayload);
  }
};

// 调用 授权 ,管辖, 科目接口 ,  启用关闭接口
const rouleDutySubjectRequest = (bodyPayload: any, methTtype: string) => {
  let reqUrl = renderReqUrlType(methTtype)['url'];
  let methodType = renderReqUrlType(methTtype)['reqType'];
  if (reqUrl && methodType) {
    return requestFunCompon(methodType, reqUrl, bodyPayload);
  }
};

// 获取授权,管辖,科目的接口路径和接口类型
const renderReqUrlType = (type: string) => {
  switch (type) {
    case 'grant': //授权角色
      return {
        // url: '/positionController/grant',
        url: '/userController/grant',
        reqType: 'PUT',
      };
    case 'setDutyScope': //设置管辖
      return {
        url: '/positionController/setDutyScope',
        reqType: 'PUT',
      };
    case 'setSubject': //设置科目
      return {
        url: '/positionController/setSubject',
        reqType: 'PUT',
      };
    case 'isOpen':
      return {
        url: '/positionController/modifyState',
        reqType: 'PUT',
      };
    default:
      return {
        url: '',
        reqType: '',
      };
  }
};

export { usePositionOperation, useViewInfoPosData, addPosRequest, rouleDutySubjectRequest };
